package cn.crc.spring;

import org.apache.tapestry.ioc.ObjectLocator;
import org.apache.tapestry.ioc.ObjectProvider;
import org.apache.tapestry.ioc.OrderedConfiguration;
import org.apache.tapestry.ioc.ServiceBinder;
import org.apache.tapestry.ioc.annotations.InjectService;
import org.apache.tapestry.services.Context;

import org.springframework.web.context.WebApplicationContext;



/**
 *  spring 1.x/2.x + Tapestry5.0.5
 * @author jun.tsai
 * @version $Revision: 2830 $
 * @since 3.0
 */
public class SpringModule {
 
	

	
	/**
	 * 
	 * */
	public static void bind(ServiceBinder binder){
		
		binder.bind(ObjectProvider.class,SpringObjectProvider.class)
		.withId("SpringObjectProvider");
	}
	
	public static WebApplicationContext buildWebApplicationContext1(@InjectService("Context") Context context){
		
		WebApplicationContext springContext = null;
		try {
			
			springContext = (WebApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
			
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(SpringMessages.failureObtainingContext(e),e);
		}
		if (springContext == null) throw new RuntimeException(SpringMessages.missingContext());
		return springContext;
	}
	
	 /**
     * Contributes a provider named "Spring".
     */
        public static void contributeMasterObjectProvider(
                        OrderedConfiguration<ObjectProvider> configuration,
                        ObjectLocator locator) {
                configuration.add("spring", locator
                                .autobuild(SpringObjectProvider.class));
        }
}
